import pymysql
from pymysql import MySQLError
import traceback
from shared.dp_config import DB_CONFIG

def log_message(log_callback, message, color=None):
    if callable(log_callback):
        log_callback(message, color) if color else log_callback(message)
def email_list(log_callback):
    try:
        connection = pymysql.Connect(**DB_CONFIG)
        print("连接成功!")

        with connection.cursor() as cursor:
            # 测试查询
            cursor.execute("SELECT * from auto_plan_email_permission")
            result = cursor.fetchall()
            column_names = [desc[0] for desc in cursor.description]
            data_list = [dict(zip(column_names, row)) for row in result]
            return [item['email'] for item in data_list]

    except MySQLError as e:
        error_msg = f"数据库连接错误: {e}"
        error_detail = traceback.format_exc()
        full_error = f"{error_msg}\n详细信息:\n{error_detail}"
        log_message(log_callback, full_error, "red")
    finally:
        if 'connection' in locals() and connection.open:
            connection.close()